import axios from "axios";

//创建axios
const service = axios.create({
    baseURL:"http://localhost:8080",
    timeout:20000 //请求超时
})

//request拦截
service.interceptors.request.use(
    config=>{
        return config;
    },
    err=>{
        console.log(err);
        Promise.reject(err);
    }
);

//response拦截
service.interceptors.response.use(
    response=>{
        let res = response.data;
        let {data,code,msg} = res;
        if(code !== 0){ //失败
            this.$message({
                message: msg,
                type: 'error',
                duration:2*1000
            });
            return false;
        }else{
            return data;
        }
    },
    err=>{
        const {status,data} = err.response;
        this.$message({
            message:data.msg,
            type:"error",
            duration:2*1000
        })
        throw new Error(data.msg);
    }
)
export default service;